package com.ac.input.web.action.input;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction;
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;
import com.ac.input.dao.BaseDao;
import com.ac.input.dao.ReportsDao;
import com.ac.input.web.form.DataSearchForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class BrowseDataAction extends BasePageViewAction {

    protected PageViewForward doPageViewExecute(ActionMapping mapping,
            BaseForm form, HttpServletRequest request,
            HttpServletResponse response, int pageNo, int pageSize)
            throws Exception {
        DataSearchForm myForm = (DataSearchForm) form;
        ReportsDao rpsDao = (ReportsDao) this.getBean("reportsDao");

        Page page = null;
        if (myForm.isFirst()) {
            myForm.setRhid(rpsDao.getHndInpDefById(myForm.getMendId()));
            myForm.setFirst(false);
        }

        String sql = myForm.getRhid().getSchSql();

        String whereStr = " where 1=1 ";
        if (myForm.getWhereStr()!=null && !"".equals(myForm.getWhereStr())) {
            whereStr += " and (" + myForm.getWhereStr() + ")";
        }
        
        String orderStr = myForm.getOrderStr();
        if (!"".equals(myForm.getOrderStr())) {
            orderStr = " order by " + myForm.getOrderStr();
        }

        String viewStr = " * ";
        BaseDao baseDao = (BaseDao) this.getBean("hndBaseDao");
        page = new Page(baseDao.getCount(sql, whereStr), pageNo, pageSize);
        List dmList = baseDao
                .getObjects(sql, viewStr, whereStr, orderStr, page);

        if("1".equals(myForm.getIsDef())){
        	String acPath = "/pages/protected/"+myForm.getAcPath()+".jsp";
    		return new PageViewForward(new ActionForward(acPath), dmList, page, myForm
    		        .getViews(), null, null);
        }
		return new PageViewForward(forward(mapping), dmList, page, myForm
		        .getViews(), null, null);
    }
}
